package org.bonorsoft.modules.dingding.util;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import org.json.JSONException;
import org.json.JSONObject;

public class DingUtil {

    private final String corpid = "dinge91442a6a05c3109ffe93478753d9884";
    public static Long agentId = 1160919791L;
    private final String AppKey = "ding7xmweiqcvhd9vydz";
    private final String AppSecret = "rhfCHxbarfkgWJC0MVrxyHEm8-Jmm1bDbALTzDZCzKc7-Ri7k-jm6dDkWqdXg6fc";
    private final String getTokenUrl = "https://oapi.dingtalk.com/gettoken?appkey=" + AppKey + "&appsecret=" + AppSecret;
    private static String userID = "";


    public String createToken() throws JSONException {
        //1、发送请求，获取tokenJson字符串
        String tokenJson = HttpClientUtils.sendGet(getTokenUrl);
        //System.out.println(tokenJson);

        //2.从json中获取token值，并付给dingToken
        JSONObject jsonObject = new JSONObject(tokenJson);
        return jsonObject.get("access_token").toString();
    }

    /**
     * 发送工作通知消息
     * 参数说明：
     *
     * @param useridList  接收者的用户userid列表，最大列表长度：20。示例：zhangsan,lisi
     * @param title       首页显示内容
     * @param markdown    进入工作通知后的标题内容
     * @param singleTitle 进入工作通知后标题下面的内容部分
     * @param singleUrl   点击内容跳转链接
     * @return 返回 taskID
     * @throws Exception
     */
    public static String sendWorkNotice(String useridList, String title, String markdown,
                                        String singleTitle, String singleUrl) throws Exception {
        DingUtil dingUtil = new DingUtil();
        String token = dingUtil.createToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");

        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setUseridList(useridList); //接收者的用户userid列表，最大列表长度：20
        request.setAgentId(agentId);
        request.setToAllUser(false); //	是否发送给企业全部用户(ISV不能设置true) 83307209

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
        msg.getActionCard().setTitle(title); //首页显示内容
        msg.getActionCard().setMarkdown(markdown); //进入工作通知后的标题内容
        msg.getActionCard().setSingleTitle(singleTitle); //进入工作通知后标题下面的内容部分
        msg.getActionCard().setSingleUrl(singleUrl); //点击内容跳转链接
        msg.setMsgtype("action_card");
        request.setMsg(msg);

        OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,token);
        //返回任务ID
        System.out.println("-------------------- String.valueOf(response.getTaskId()) : " + String.valueOf(response.getTaskId()));
        return String.valueOf(response.getTaskId());
    }
}
